package com.zhanggm.ry.order.controller;


import com.ruoyi.common.core.web.domain.AjaxResult;
import com.zhanggm.ry.order.entity.Order;
import com.zhanggm.ry.order.feign.StockFegin;
import com.zhanggm.ry.order.service.IOrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhanggm
 * @since 2023-04-25
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    private int i = 0;

    private Lock lock = new ReentrantLock();

    @Autowired
    private IOrderService orderService;

    @Autowired
    private StockFegin stockFegin;


    @GetMapping("add")
    @GlobalTransactional
    public synchronized AjaxResult add() {
        i++;
        //下单
        Order order = new Order();
        order.setTotalPay(100L);
        order.setActualPay(100L);
        order.setPaymentType(true);
        order.setPostFee(0L);
        order.setUserId(1 + "");
        order.setBuyerNick("lisi");
        orderService.save(order);
        //远程调用库存服务，减库存
        stockFegin.reduce(2600242L,1);
        return AjaxResult.success();
    }




}
